package com.googlecode.contourplot;

/* ===========================================================
 * JFreeChart : a free chart library for the Java(tm) platform
 * ===========================================================
 *
 * (C) Copyright 2000-2004, by Object Refinery Limited and Contributors.
 *
 * Project Info:  http://www.jfree.org/jfreechart/index.html
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 *
 * ---------------------
 * ContourPlotDemo2.java
 * ---------------------
 * (C) Copyright 2003, 2004, by David M. O'Donnell and Contributors.
 *
 * Original Author:  David M. O'Donnell;
 * Contributor(s):   David Gilbert (for Object Refinery Limited);
 *
 * $Id: ContourPlotDemo2.java,v 1.18 2004/04/30 08:05:59 mungady Exp $
 *
 * Changes
 * -------
 * 22-Apr-2003 : Added standard header (DG);
 *
 */

import java.util.Date;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ColorBar;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.data.contour.DefaultContourDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RefineryUtilities;

/**
 * A demonstration application to illustrate ContourPlot. Command line options
 * exist to control different plot properties such as colorbar orientation, etc.
 * List of options are available by launching with the -? option, e.g.,
 * ContourPlotDemo -?
 * 
 * @author DMO
 */
@SuppressWarnings("deprecation")
public class ContourPlotDemo2 extends ApplicationFrame {
	private static final long serialVersionUID = 131718880927057524L;

	/** The x axis. */
	private ValueAxis xAxis = null;

	/** The y axis. */
	private NumberAxis yAxis = null;

	/** The z axis. */
	private ColorBar zColorBar = null;

	/** The plot ratio. */
	private static double ratio = 0.0;

	/** Temp data storage. */
	private double[] tmpDoubleY = null;

	/** Temp data storage. */
	private double[] tmpDoubleX = null;

	/** Temp data storage. */
	private double[] tmpDoubleZ = null;

	/**
	 * Constructs a new demonstration application.
	 * 
	 * @param title
	 *            the frame title.
	 */
	public ContourPlotDemo2(final String title) {

		super(title);

		final JFreeChart chart = createContourPlot();
		final ChartPanel panel = new ChartPanel(chart, true, true, true, true,
				true);
		panel.setPreferredSize(new java.awt.Dimension(1000, 800));
		panel.setMaximumDrawHeight(100000);
		panel.setMaximumDrawWidth(100000);
		panel.setFillZoomRectangle(true);
		setContentPane(panel);

	}

	/**
	 * Creates a ContourPlot chart.
	 * 
	 * @return the ContourPlot chart.
	 */
	private JFreeChart createContourPlot() {

		final String title = "Contour Plot";
		final String xAxisLabel = "X Values";
		final String yAxisLabel = "Y Values";
		final String zAxisLabel = "Color Values";

		this.xAxis = new NumberAxis(xAxisLabel);
		this.yAxis = new NumberAxis(yAxisLabel);
		this.zColorBar = new ColorBar(zAxisLabel);

		if (this.xAxis instanceof NumberAxis) {
			((NumberAxis) this.xAxis).setAutoRangeIncludesZero(false);
		}

		this.yAxis.setAutoRangeIncludesZero(false);

		((NumberAxis) this.xAxis).setLowerMargin(0.0);
		((NumberAxis) this.xAxis).setUpperMargin(0.0);

		this.yAxis.setLowerMargin(0.0);
		this.yAxis.setUpperMargin(0.0);

		this.xAxis.setRange(0.0, 3.0);
		this.yAxis.setRange(0, 2.0e8);

		this.zColorBar.getAxis().setTickMarksVisible(true);

		final ContourPlotDataSeries data = createDataset();

		final ContourShapePlot plot = new ContourShapePlot(data, this.xAxis,
				this.yAxis, this.zColorBar, ContourRenderType.ColorRenderType);
		plot.setColorBarLocation(RectangleEdge.BOTTOM);

		plot.setDataAreaRatio(ratio);

		final JFreeChart chart = new JFreeChart(title, null, plot, false);

		return chart;

	}

	/**
	 * Creates a ContourDataset.
	 * 
	 * @return ContourDataset.
	 */
	private ContourPlotDataSeries createDataset() {
		initData();

		final Double[] oDoubleX = (Double[]) DefaultContourDataset
				.formObjectArray(this.tmpDoubleX);
		final Double[] oDoubleY = (Double[]) DefaultContourDataset
				.formObjectArray(this.tmpDoubleY);
		final Double[] oDoubleZ = (Double[]) DefaultContourDataset
				.formObjectArray(this.tmpDoubleZ);

		final Date[] tmpDateX = new Date[this.tmpDoubleX.length];
		for (int i = 0; i < this.tmpDoubleX.length; i++) {
			tmpDateX[i] = new Date((long) (1000.0 * this.tmpDoubleX[i]));
		}

		return new ContourPlotDataSeries("Contouring", oDoubleX, oDoubleY,
				oDoubleZ, 15, 15);

	}

	/**
	 * Starting point for the demonstration application.
	 * 
	 * @param args
	 *            command line options, launch ContourDemoPlot -? for listing of
	 *            options.
	 */
	public static void main(final String[] args) {
		final ContourPlotDemo2 demo = new ContourPlotDemo2("ContourPlot Demo");
		demo.pack();
		RefineryUtilities.centerFrameOnScreen(demo);
		demo.setVisible(true);

	}

	/**
	 * Initialise data.
	 */
	private void initData() {

		this.tmpDoubleY = new double[ContourTestData.Xval.length];
		this.tmpDoubleX = new double[ContourTestData.Xval.length];
		this.tmpDoubleZ = new double[ContourTestData.Xval.length];

		for (int i = 0; i < this.tmpDoubleX.length; i++) {
			this.tmpDoubleX[i] = ContourTestData.Xval[i];
			this.tmpDoubleY[i] = ContourTestData.Yval[i];
			this.tmpDoubleZ[i] = ContourTestData.Zval[i];
		}
	}

}
